<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/24
 * Time: 15:16
 */
namespace Home\Model;
class woZanPay{
    protected $appid = 'A9B1D381123BCE97689BD0073A4AA43D5';
    public $open_key = 'abbbf05de660ac20b465212c8e2ea44b';
    public $product_no = 'P000119';
    protected $value = array();

    public function order(){
        $this->getJson();
        $this->getExt();
        $this->getSign();
        $url = 'https://pay.szwzpay.com/pay/order?appID='.$this->appid.'&json='.$this->json.'&ext='.$this->ext.'&sign='.$this->sign;
        $header = array('Language:php');
        $res = $this->http_request($url,$header);
        return $res;
    }

    public function getSign(){

        $openKey = $this->open_key."&";
        $openKey = mb_convert_encoding($openKey, "UTF-8");
        $key = $this->appid.'&'.$this->json.'&'.$this->ext;

        $key = mb_convert_encoding($key, "UTF-8");
        $str = hash_hmac('sha1',$key,$openKey,true);
        $this->sign = (base64_encode($str));
    }

    public function getJson(){
        $this->json =  base64_encode(json_encode($this->value));
    }

    public function getExt(){
        $this->ext = json_encode($this->ex);
    }

    public function setExt($key,$value){
        $this->ex[$key] = $value;
    }


    public function setValue($key,$value){
        $this->value[$key] = $value;
    }




    protected function http_request($url,$header = null, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if(!empty($header)){
            curl_setopt($curl, CURLOPT_HTTPHEADER,$header);
        }

        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        $output = curl_exec($curl);
        curl_close($curl);
        // var_dump($output);
        return $output;
    }


}